//
//  RolesViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/4/14.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "RolesViewController.h"
#import "PositionSelectViewController.h"
#import "CityModel.h"
#import "SelectCityViewController.h"
#import "SalarySelectViewController.h"
#import "SearchCompanyViewController.h"
#import "NSUDManager.h"
#import "AppDelegate.h"

#import "CompanyInfoCertificateVC.h"
#import "CompanyModel.h"
#import "CertificationViewController.h"
#import "CompanyInfoCertificateVC.h"

@interface RolesViewController ()

@property (nonatomic, strong) JobModel *selectJob;
@property (nonatomic, strong) CityModel *selectCity;
@property (nonatomic, strong) CompanyModel *selectCompany;
@property (nonatomic, copy) NSString *minSalary;
@property (nonatomic, copy) NSString *maxSalary;

@end

@implementation RolesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    
    
    self.bgImageHeight.constant = 467 * kScreenWidth / 750;
    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.shadowV.bounds];
    self.shadowV.layer.shadowColor = [UIColor blackColor].CGColor;
    self.shadowV.layer.shadowOffset = CGSizeMake(0, 0);
    self.shadowV.layer.shadowOpacity = 0.2;
    self.shadowV.layer.shadowRadius = 10.f;
    self.shadowV.layer.shadowPath = shadowPath.CGPath;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SearchCityBlock:) name:@"searchcity" object:nil];
}


- (IBAction)jobBtnClick:(id)sender {
    PositionSelectViewController *psVC = [[PositionSelectViewController alloc] init];
    psVC.selectBlock = ^(JobModel *job) {
        _selectJob = job;
        self.jobL.text = job.jobName;
    };
    [self.navigationController pushViewController:psVC animated:YES];
}

- (IBAction)cityBtnClick:(id)sender {
    SelectCityViewController *scVC = [[SelectCityViewController alloc] init];
    scVC.cityBlock = ^(CityModel *city) {
        self.selectCity = city;
        self.cityL.text = city.regionName;
    };
//    [self.navigationController pushViewController:scVC animated:YES];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)SearchCityBlock:(NSNotification *)notification {
    // 得到传递的参数
    NSDictionary *dict = [notification userInfo];
    // 重新选择收货地址后，走此条件
    CityModel *city = [dict objectForKey:@"city"];
    self.selectCity = city;
    self.cityL.text = city.regionName;
}

- (IBAction)salaryBtnClick:(id)sender {
    SalarySelectViewController *salary = [[SalarySelectViewController alloc] init];
    WS(weakSelf)
    salary.selectBlock = ^(NSString *minSalary, NSString *maxSalary) {
        weakSelf.minSalary = [minSalary substringToIndex:minSalary.length - 1];
        weakSelf.maxSalary = [maxSalary substringToIndex:maxSalary.length - 1];
        weakSelf.salaryL.text = [NSString stringWithFormat:@"%@-%@", minSalary, maxSalary];
    };
    // 核心代码
    self.definesPresentationContext = YES;
    salary.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self.navigationController presentViewController:salary animated:NO completion:nil];
}

- (IBAction)companyBtnClick:(id)sender {
    SearchCompanyViewController *scVC = [[SearchCompanyViewController alloc] init];
    scVC.index = 1;
    scVC.companyBlock = ^(CompanyModel *company) {
        self.selectCompany = company;
        self.companyL.text = company.companyAbbreviatedName;
    };
    [self.navigationController pushViewController:scVC animated:YES];
}

- (IBAction)incumbentBtnClick:(id)sender {
    self.incumbentBtn.selected = !self.incumbentBtn.selected;
}

- (IBAction)okBtnClick:(id)sender {
    if (!self.selectJob) {
        [CommonUtils showToast:@"请选择期望职位"];
        return;
    }
    if (!self.selectCity) {
        [CommonUtils showToast:@"请选择意向城市"];
        return;
    }
    if ([self.salaryL.text isEqualToString:@"请选择"]) {
        [CommonUtils showToast:@"请选择薪资要求"];
        return;
    }
//    if (!self.selectCompany) {
//        [CommonUtils showToast:@"请选择目标公司"];
//        return;
//    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.selectCompany) {
        params[@"targetCompany"] = self.selectCompany.companyId;
    }
    params[@"jobId"] = self.selectJob.jobId;
    params[@"region"] = self.selectCity.regionId;
    params[@"minIntentionalSalary"] = self.minSalary;
    params[@"maxIntentionalSalary"] = self.maxSalary;
    
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest registerIdentityInfomationWithCandidateParam:params result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"注册用户身份信息成功");
            if (weakSelf.incumbentBtn.selected) {
                UserModel *user = [NSUDManager currentUser];
                user.role = @"2";
                [NSUDManager setCurrentUser:user];
    
                UIWindow *win = [UIApplication sharedApplication].delegate.window;
//                CertificationViewController *certifiVC = [[CertificationViewController alloc] init];
                
                CompanyInfoCertificateVC *certifiVC = [[CompanyInfoCertificateVC alloc] init];
                certifiVC.index = 1;
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:certifiVC];
                win.rootViewController = nav;
                [win makeKeyAndVisible];
            } else {
                UserModel *user = [NSUDManager currentUser];
                user.role = @"1";
                [NSUDManager setCurrentUser:user];
                
               [((AppDelegate *)[UIApplication sharedApplication].delegate) gotoHome];
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

@end
